package com.inspur.edp.common.expr.expresses.variable;


import com.inspur.edp.common.expr.Express;
import com.inspur.edp.common.expr.ExpressDSLGenerator;
import com.inspur.edp.common.expr.ExpressDSLGeneratorMgr;
import com.inspur.edp.common.expr.ast.ASTContext;

public class VariableDefDSL implements ExpressDSLGenerator {

    @Override
    public String getKind() {
        return VariableDefExpr.KIND;
    }

    @Override
    public String generate(ASTContext context, Express express) {
        VariableDefExpr variableExpr = (VariableDefExpr) express;
        StringBuilder sb = new StringBuilder();
        context.addVariable(variableExpr.getName(), variableExpr.getType());
        sb.append(String.format("let %s : %s ", variableExpr.getName(), variableExpr.getType().getText()));
        context.addImports(variableExpr.getType().getImports());
        if (variableExpr.getValue() != null) {
            sb.append(" = " + ExpressDSLGeneratorMgr.generateDsl(context, variableExpr.getValue()));
        }
        return sb.toString();
    }
}
